package com.edu.hre.db.sql.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreJobCategory;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.admin.JobCategoryForm;
import com.edu.hre.struts.form.admin.JobCategoryListForm;

public class JobCategoryDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "jobCategoryDAOService";
	private BaseService baseService  = null;

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<JobCategoryForm> getJobCategoryListInfo(int limit, int offset){

		List<HreJobCategory> hreJobCategories = baseService.getPojos(null, -1, null, new String[]{"dateCreated"}, new Boolean[]{true}, limit, offset, HreJobCategory.class);
		ArrayList<JobCategoryForm> jobCategories = new ArrayList<JobCategoryForm>();

		if (hreJobCategories == null) {
			return jobCategories;
		}
		for (HreJobCategory hreJob: hreJobCategories) {
			
			try {

				JobCategoryForm jobCategory = new JobCategoryForm();
				jobCategory.setId(hreJob.getId() + "");
				jobCategory.setJobCategory(CommonUtils.normalizeString(hreJob.getName()));

				jobCategories.add(jobCategory);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return jobCategories;
	}
	
	public int countJobCategories(){

		int count = baseService.countByColumns(null, null, null, "id", HreJobCategory.class);
		return count < 0 ? 0 : count;
	}

	public void deleteJobCategories(JobCategoryListForm form){

		ArrayList<JobCategoryForm> info = (ArrayList<JobCategoryForm>) form.getAllCategories();
		
		for (int i = 0; i < info.size(); i++) {
			
			try {

				JobCategoryForm category = info.get(i);
				if (category.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, Integer.parseInt(category.getId()), null, null, 0, HreJobCategory.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
